package com.situ.mall.common.util;


import com.situ.mall.common.entity.Member;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class RequestUtil {
    private  static final String CURRENT = "currentMember";

    public static HttpServletRequest getRequest(){
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }
    public static HttpSession getSession(){
        return getRequest().getSession();
    }

    public static void removeSession(){
        getSession().removeAttribute("CURRENT");
    }

    //保存当前用户进session
    public static void setCurrent(Member member){

      //
      getSession().setAttribute(CURRENT,member);
    }
    public static Member getAdmin() {
        //Object obj=getSession().getAttribute(CURRENT);
        Object obj=getSession().getAttribute(CURRENT);
        if(obj==null){
            return null;
        }
        return (Member) obj;
    }

    public static Member getCurrent() {

//        return (Member) getRequest().getSession().getAttribute(CURRENT);
        return (Member) getRequest().getSession().getAttribute(CURRENT);
    }
}
